package net.huashitong.common.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.common.entity.SysTrouble;
import net.huashitong.common.filter.SysTroubleFilter;
import net.huashitong.common.service.SysTroubleService;
import net.huashitong.util.UtilsTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 招商 经济 重大 系统  协商 汇总
 * @author
 */
@Controller
@RequestMapping(value = "/common/trouble")
public class SysTroubleController extends JsonBaseController {
    @Autowired
    private SysTroubleService sysTroubleService;
    @Override
    protected String getView(String s) {
        return "/modules/common/trouble/"+s;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initPage(Model model) {
        return getView("query");
    }
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid iniPageData(SysTroubleFilter filter) {
        List<SysTrouble> troubleList = sysTroubleService.queryByFilter(filter);
        return coverJqGrid(filter,troubleList);
    }
    @RequestMapping("/filter")
    public String filter(@ModelAttribute SysTroubleFilter filter) {
        return getView("filter");
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable String id) {
        SysTrouble sysTrouble = sysTroubleService.getById(id);
        model.addAttribute("SysTroubleFilter",sysTrouble);
        return getView("form");
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message update(@ModelAttribute SysTroubleFilter filter) {
        if (StringUtils.isBlank(filter.getSysId())) {
            return coverMessage(UtilsTools.ERROR,"困难事件不存在");
        }
        SysTrouble sysTrouble = new SysTrouble();
        UtilsTools.copyPro(filter,sysTrouble);
        sysTroubleService.update(sysTrouble);
        return coverMessage(UtilsTools.CODE);
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAddView(@ModelAttribute SysTroubleFilter filter , Model model) {
        model.addAttribute("SysTroubleFilter",filter);
        return getView("form");
    }
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute SysTroubleFilter filter) {
        SysTrouble sysTrouble = new SysTrouble();
        UtilsTools.copyPro(filter,sysTrouble);
        sysTroubleService.save(sysTrouble);
        return coverMessage(UtilsTools.CODE);
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}")
    public Message deleteTrouble(@PathVariable List<String> id){
        sysTroubleService.batchDelete(id);
        return coverMessage(UtilsTools.CODE);
    }



}
